//
//  ZJQASharchView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/22.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJQASharchView.h"

@interface ZJQASharchView ()<UITextFieldDelegate>
{
    UIView *_bgView;
}
/** imageView */
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation ZJQASharchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(25 * [UIScreen zj_screenWScale], 8 * [UIScreen zj_screenHScale], self.width - 50 * [UIScreen zj_screenWScale], self.height - 16 * [UIScreen zj_screenWScale])];
        _bgView.backgroundColor = [UIColor zj_colorWithHexString:@"ebebeb"];
        _bgView.layer.borderWidth = 1;
        _bgView.layer.borderColor = [UIColor zj_colorWithHexString:@"c9c9c9"].CGColor;
        _bgView.layer.cornerRadius = _bgView.height / 2;
        [self addSubview:_bgView];
        [self addSubview:self.textField];
        [self addSubview:self.imageView];
        //分割线
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, self.height - 1, self.width, 1)];
        line.backgroundColor = [UIColor zj_colorWithHexString:@"ebebeb"];
        [self addSubview:line];
    }
    return self;
}


//#pragma mark - UITextFiled 协议方法
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//    
//    if (toBeString.length >= 50 && range.length!= 1 ){
//        textField.text = [toBeString substringToIndex:50];
//        return NO;
//        
//    }
//    return YES;
//}
//
//- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//    [textField resignFirstResponder];
//    if (self.delegate) {
//        [self.delegate ZJQASharchView_delegate_search];
//    }
//    return YES;
//}
//
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//    if (self.delegate) {
//        [self.delegate ZJQASharchView_delegate_search];
//    }
//}

- (void)tapSearch{
    if (self.delegate) {
        [self.delegate ZJQASharchView_delegate_search];
    }
}

#pragma mark - Setter
#pragma mark - Getters
- (UITextField *)textField{
    if (_textField == nil){
        _textField = [[UITextField alloc]initWithFrame:CGRectMake(_bgView.x + 15 * [UIScreen zj_screenWScale], _bgView.y, _bgView.width - 25 * [UIScreen zj_screenWScale] - _bgView.height, _bgView.height)];
        _textField.font = [UIFont subTitleTextFont];
        _textField.textColor = [UIColor textBlackColor];
        _textField.placeholder = @"在此搜索您可能需要的问题答案";
//        _textField.delegate = self;
        _textField.returnKeyType = UIReturnKeySearch;
    }
    return _textField;
}

- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [[UIImageView  alloc]initWithFrame:CGRectMake(_bgView.right - 10 * [UIScreen zj_screenWScale] - _bgView.height, _bgView.y, _bgView.height, _bgView.height)];
        _imageView.contentMode = UIViewContentModeCenter;
        _imageView.image = [UIImage imageNamed:@"icon_qa_soso"];
        _imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSearch)];
        [_imageView addGestureRecognizer:tap];
    }
    return _imageView;
}

@end
